$(document).ready(function(){
	var goodsTable=$('#goodsTable').datagrid({
		fit:true,
		url:js+'dict/viewGoods.action',
		sortName: 'goodsID',
		idField:'goodsID',
		fitColumns: true,
		height: 'auto',
		columns:[[{field:'ck',checkbox:true},
			{field:'goodsName',title:'名称',width:120,sortable:true},
			{field:'goodsCode',title:'编号',width:120,sortable:true},
			{field:'goodsType',title:'货物类型',width:120,sortable:true},
			{field:'dangerous',title:'危险品',width:120,sortable:true},
			{field:'unitName',title:'度量单位',width:120,sortable:true},
			{field:'note',title:'备注',width:120,sortable:true},
			{field:'handlerName',title:'创建者',width:150},
			{field:'handlerTime',title:'创建时间',width:150},
			{field:'operatorName',title:'最后修改者',width:150},
			{field:'operateTime',title:'最后修改时间',width:150}
		]],
		pagination:true,
		rownumbers:true,
		onDblClickRow:function(rowIndex, rowData ){
			$('#addGoodsDialog').dialog('open').dialog('setTitle','修改货物');
			$('#addGoodsDialog').form('load',rowData);
			url = js+'dict/updateGoods.action'
		}
	});
	
	$('#add').click(function(){
		
		$('#addGoodsDialog').dialog('open').dialog('setTitle','新增货物');
		$('#addGoodsDialog').form('clear');
		url = js+'dict/addGoods.action';
		return false;
	})	
	$('#addGoodsDialog').dialog({
				toolbar:[{
					text:'保存',
					iconCls:'icon-save',
					handler:function(){
						$('#addGoodsForm').form('submit',{
		    				url: url,
		    				onSubmit: function(){
			    				if($('#goodsTypeID').val()==null||$('#goodsTypeID').val()==''){
			    					$.messager.alert('提示：','请选择货物类型','error');
			    					return false;
			    				}
			    				if($('#unitID').val()==null||$('#unitID').val()==''){
			    					$.messager.alert('提示：','请选择单位','error');
			    					return false;
			    				}
		    					 if($(this).form('validate')){
		    					 	return true;
		    					 }else{
		    					 	$.messager.alert('提示：','请填写完整信息','error');
		    					 	return false;
		    					 }
		    				},
		    				success: function(data){
		    					var result = eval('('+data+')');
		    					if (result.success){
		    						$('#goodsTable').datagrid('reload');
		    						$('#addGoodsDialog').dialog('close');
		    						$('#addGoodsForm').dialog('clear');
		    						
		    						
		    					} else {
	    							$.messager.alert('提示：','编辑货物不成功','error');
		    					}
		    				}
		    			});
					}
				},'-',
				{
					text:'取消',
					iconCls:'icon-cancel',
					handler:function(){
						$('#addGoodsDialog').dialog('close');
						$('#addGoodsForm').dialog('clear');
					}
				}
				]
			});	
			
			
			/*删除用户*/
	$("#detele").click(function(){
		var idsArray = new Array();
		var rows = $('#goodsTable').datagrid('getSelections');
		if(rows.length==0){
			$.messager.alert("提示","请选中数据","warning");
			return false;
		}
		$.messager.confirm("提示！","确定要删除选中货物?",function(t){ 
			if(!t) return;
			for(var i=0;i<rows.length;i++){
				idsArray.push(rows[i].goodsID);
			}	
			var ids= idsArray.join(",");
			$.post(js+"dict/deleteByIDsGoods.action",{ids:ids},function(result){
			     if(result.success){
			     	var rows = $('#goodsTable').datagrid('getSelections');
			     	while(rows&&rows.length>0){
						var index = $('#goodsTable').datagrid('getRowIndex', rows[0]);
						$('#goodsTable').datagrid('deleteRow', index);
						rows = $('#goodsTable').datagrid('getSelections');
					}
			      }else{
			        $.messager.alert('提示',"删除不成功","error");	
			      }
			},"json");
			
		});
		
		return false;
	});
	
})
function lookup(value,name){
	var searchUrl;
	if('all'==name){
		value='';	
		searchUrl = js+'dict/viewGoods.action?searchInput='+value;
	}else if('goodsCode'==name){
		searchUrl = js+'dict/viewGoods.action?searchInput='+value+"&searchKind=goodsCode";
	}else if('goodsName'==name){
		searchUrl = js+'dict/viewGoods.action?searchInput='+value+"&searchKind=goodsName";
	}
	$('#goodsTable').datagrid({
		 url:searchUrl,
    });
}